<template>
  <div class="goods">
    <div class="wrap">
      <Crumb :navs="nav"> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            :class="{ active: indexActive == index }"
            v-for="(item, index) in options1"
            :key="index"
            @click="options1Fn(index, item.min, item.max)"
            >{{ item.txt }}</span
          >
        </li>
        <li>
          <strong>分类：</strong>
          <span
            :class="{ active: item.isActrve }"
            v-for="(item, index) in options2"
            :key="index"
            @click="options2Fn(index, item.type)"
            >{{ item.txt }}</span
          >
        </li>
      </ul>
      <List :arr="list" />
    </div>
  </div>
</template>                            
<script>
import { GoosSearchAPI } from "@/request/api";
import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
export default {
  data() {
    return {
      list: [],
      nav: [
        { link: "#", name: "首页" },
        { link: "#", name: "全部商品" },
        { link: "#", name: "商品详情" },
      ],
      dis: 0,
      type: 0,
      min: 0,
      max: 0,
      keyword: "",
      // 排序的数组项
      options1: [
        { min: 0, max: 10000, txt: "全部" },
        // "我还可以兑换"的max值是Header组件的coin值
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      // 分类数组项
      options2: [
        { type: 0, txt: "全部", isActrve: true },
        { type: 1, txt: "实体商品", isActrve: false },
        { type: 2, txt: "虚拟商品", isActrve: false },
      ],
      indexActive: 0,
      indexClass: 0,
    };
  },
  components: {
    Crumb,
    List,
  },
  methods: {
    // 请求封装
    async getGoosSeaerch() {
      let res = await GoosSearchAPI({
        did: this.did,
        type: this.type,
        min: this.min,
        max: this.max,
        keyword: this.keyword,
      });
      this.list = res.data;
      // this.$store.state.list = res.data;
    },
    options1Fn(index, min, max) {
      this.indexActive = index;
      (this.min = min), (this.max = max);
      this.getGoosSeaerch();
    },
    options2Fn(index, type) {
      // 实现高亮的第一种方法
      // (this.indexClass = index),
      // 实现高亮方式二   排他
      // 1.让所有都变成false
      this.options2.forEach((item) => (item.isActrve = false));
      // 2.让自己变成true
      this.options2[index].isActrve = true;
      this.type = type;
      this.getGoosSeaerch();
    },
  },
  created() {
    // console.log(this.$route.query.keyword);
    this.keyword = this.$route.query.keyword || "";
    this.getGoosSeaerch();
  },
  watch: {
    "$route.query.keyword": {
      handler(n, o) {
        this.keyword = n || "";
        this.getGoosSeaerch();
      },
    },
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/css/public.less";

.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
}
</style>